package com.project.pdf;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

import swings.mycomponent.panel.JStatusBarPanel;

/**
 * @Title: SwingWorkerWinApp.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2011-3-8
 * @version V1.0
 */

public class SwingWorkerWinApp {

	private static Logger log = Logger.getLogger(SwingWorkerWinApp.class);

	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("com.project.pdf.messages"); //$NON-NLS-1$

	private JFrame frame;
	private final JPanel footpanel = new JStatusBarPanel(
			BUNDLE.getString("frame.footpanel.left"),
			BUNDLE.getString("frame.footpanel.right"));

	private static Desktop desktop;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
		} else {
			System.out.println("Desktop class is not supported");
			System.exit(1);
		}

		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingWorkerWinApp window = new SwingWorkerWinApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SwingWorkerWinApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		// 基本设置
		frame = new JFrame();
		frame.setResizable(false);
		frame.setIconImage(Toolkit.getDefaultToolkit()
				.getImage(
						SwingWorkerWinApp.class
								.getResource("/com/project/pdf/source/pdf.png")));

		frame.setTitle(BUNDLE.getString("frame.title")); //$NON-NLS-1$
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 窗口居中
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getCenterPoint();
		int windowWidth = 450;
		int windowHeight = 300;

		frame.setBounds(center.x - windowWidth / 2,
				center.y - windowHeight / 2, windowWidth, windowHeight);

		// 引入定制好的 Jpanel
		SwingWorkerJPanel panel = new SwingWorkerJPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		frame.getContentPane().add(footpanel, BorderLayout.SOUTH);

		// 定义菜单栏
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu(BUNDLE.getString("menu.file"));
		menuBar.add(fileMenu);

		JMenuItem exitMenuItem = new JMenuItem(
				BUNDLE.getString("menu.file.exit"));
		exitMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});

		fileMenu.add(exitMenuItem);

		JMenu helpMenu = new JMenu(BUNDLE.getString("menu.help"));
		menuBar.add(helpMenu);

		/**
		 * 创建 aboutMenu 菜单项
		 */

		JMenuItem helpMenuItem = new JMenuItem(BUNDLE.getString("menu.help"));
		helpMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					// String uripath = SwingWorkerWinApp.class.getClassLoader()
					// .getResource("").getPath()
					// + "com/info/doc/help.html";
					// uripath = FilenameUtils.separatorsToSystem(uripath);
					// uripath = new File(uripath).toURI().toString();
					// log.info("open :" + uripath);

					URI browseURI = new URI(BUNDLE.getString("menu.help.url"));
					desktop.browse(browseURI);
				} catch (Exception ex) {
					System.out.println(ex.getMessage());
				}

			}
		});
		helpMenu.add(helpMenuItem);

		JMenuItem regMenuItem = new JMenuItem("注 册");
		regMenuItem.setEnabled(false);
		regMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});

		helpMenu.add(regMenuItem);

		JMenuItem aboutMenuItem = new JMenuItem(
				BUNDLE.getString("menu.help.about")); //$NON-NLS-1$
		aboutMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				System.out.println("aboutMenu was clicked...");

				JOptionPane.showMessageDialog(
						frame,
						BUNDLE.getString("menu.help.about.about"),
						"About",
						JOptionPane.INFORMATION_MESSAGE,
						new ImageIcon(
								SwingWorkerWinApp.class
										.getResource("/com/project/pdf/source/java-duke-logo_80x80.png")));

			}
		});

		JSeparator separator = new JSeparator();
		helpMenu.add(separator);
		helpMenu.add(aboutMenuItem);

	}
}
